package com.ctcemti.mdm.common.util;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

public class OutHtml {
    /**
     * 输出HTML脚本
     */
    public static void writeJson(HttpServletResponse response, Object obj) {
        writeData(response, obj, "UTF-8");
    }

    /**
     * 输出HTML脚本带格式
     */
    public static void writeData(HttpServletResponse response,Object obj,String chartset){
        PrintWriter out = null;
        try {
            out = response.getWriter();
            String myobj = JSON.toJSONString(obj);

            response.setContentType("text/html;charset="+chartset);//设置客户端内容类型
            response.setDateHeader("Expires", -10);
            response.setCharacterEncoding(chartset);//第一句，设置服务器端编码
            out.write(myobj);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(out!=null){
                out.close();
            }
        }
    }
}
